package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Employees;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

public class EmployeeDAO extends DAO {

    public EmployeeDAO() {
        super();
    }

    @SuppressWarnings("unchecked")
    public List<Employees> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        return criteria.list();
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        return ((Integer) criteria.list().get(0)).intValue();
    }

    @SuppressWarnings("unchecked")
    public List<Employees> getManagers() throws Exception {
        return (List<Employees>) getSession().createQuery("from Employees e join e.usertypeEmployees ue join ue.usertypes u where u.usertypeid=9").setCacheable(true).list();
    }

    @SuppressWarnings("unchecked")
    public String validateEmployee(String id, String password) throws Exception {
        Employees theEmployee = (Employees) getSession().createQuery("from Employees e where e.idnumber=:id and e.password=:t").setCacheable(true).setParameter("id", id).setParameter("t", password).setMaxResults(1).uniqueResult();
        if (theEmployee != null) {
            if (!theEmployee.getPassword().equals(password)) {
                return "incorrect";
            }
        } else {
            return "none";
        }
        return "ok";
    }

    @SuppressWarnings("unchecked")
    public Employees getByIdNumber(String eid) throws Exception {
        return (Employees) getSession().createQuery("from Employees e where e.idnumber=:eid").setParameter("eid", eid).setCacheable(true).setMaxResults(1).uniqueResult();
    }

    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String empid) throws Exception {
        Employees e = (Employees) getSession().createQuery("from Employees e where e.idnumber=:id").setParameter("id", empid).setCacheable(true).uniqueResult();
        if (e == null) {
            return false;
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    public int nextEmployeeId() throws Exception {
        DetachedCriteria dc = DetachedCriteria.forClass(Employees.class);
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.max("employeeid"));
        return ((Integer) criteria.list().get(0)).intValue() + 1;
    }

    @SuppressWarnings("unchecked")
    public Employees get(int id) throws Exception {
        return (Employees) getSession().createQuery("from Employees e where e.employeeid=:id").setParameter("id", id).uniqueResult();
    }

    @SuppressWarnings("unchecked")
    public void update(Employees e) throws Exception {
        begin();
        try {
            getSession().update(e);
            commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public void save(Employees e) throws Exception {
        begin();
        try {
            getSession().save(e);
            commit();
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }
    
    
}
